<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                               |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionBack\Logic;


class OilPayOrderLogic extends BaseLogic
{
    protected $autoCheckFields = false;

    protected $oilPayOrderModel;

    public function _initialize()
    {
        $this->oilPayOrderModel = D('OilPayOrder', 'Model');
    }

    /**
     * @param       $page
     * @param       $pageSize
     * @param array $params
     *
     * @return array
     */
    public function getOrderList($page, $pageSize, $params = [])
    {
        $condition = [];
        if ($params['status']) {
            if ($params['status'] == 2) {
                $condition['o.status'] = ['between', [2, 3]];
            } else {
                $condition['o.status'] = $params['status'];
            }
        }

        if ($params['order_sn']) {
            $condition['o.order_sn'] = $params['order_sn'];
        }

        if ($params['juhe_order_sn']) {
            $condition['o.juhe_order_sn'] = $params['juhe_order_sn'];
        }

        if ($params['pay_code']) {
            $condition['o.pay_code'] = $params['pay_code'];
        }

        if ($params['card_type']) {
            $condition['o.card_type'] = $params['card_type'];
        }

        if ($params['card_num']) {
            $condition['o.card_num'] = $params['card_num'];
        }

        if ($params['nick_name']) {
            $condition['u.nick_name'] = $params['nick_name'];
        }


        if ($params['start_time'] && $params['end_time']) {
            $condition['o.create_time'] = ['between', [strtotime($params['start_time']), strtotime($params['end_time'])]];
        } elseif ($params['start_time']) {
            $condition['o.create_time'] = ['gt', strtotime($params['start_time'])];
        } elseif ($params['end_time']) {
            $condition['o.create_time'] = ['lt', strtotime($params['end_time'])];
        }

        $total = $this->oilPayOrderModel->alias('o')
            ->join('left join clcw_user u on o.user_id=u.user_id')
            ->where($condition)
            ->count();

        $cols = 'o.id,o.order_sn,o.juhe_order_sn,o.pay_sn,o.pay_code,o.money,o.status,o.card_type,
                 o.card_num,o.user_id,u.nick_name,o.create_time,o.return_time';

        $list = $this->oilPayOrderModel->alias('o')->field($cols)
            ->join('left join clcw_user u on o.user_id=u.user_id')
            ->where($condition)
            ->page($page, $pageSize)
            ->select();

        return [
            'total' => $total,
            'list'  => $list
        ];

    }

    /**
     * @param        $id
     * @param string $field
     *
     * @return mixed
     */
    public function getOrderById($id, $field = '*')
    {
        return $this->oilPayOrderModel->field($field)->where(['id' => $id])->find();
    }

    /**
     * @param $sn
     *
     * @return mixed
     */
    public function getOrderBySn($sn)
    {
        return $this->oilPayOrderModel->where(['order_sn' => $sn])->find();
    }

    /**
     * @param $sn
     * @param $data
     *
     * @return mixed
     */
    public function saveOrderBySn($sn, $data)
    {
        return $this->oilPayOrderModel->where(['order_sn' => $sn])->save($data);
    }

    /**
     * @param $id
     * @param $data
     *
     * @return mixed
     */
    public function saveOrderById($id, $data)
    {
        return $this->oilPayOrderModel->where(['id' => $id])->save($data);
    }

    /**
     * 获取用户加油卡充值成功记录
     *
     * @param int $uid
     */
    public function getOilOrderListByUid($uid = 0)
    {
        if (empty($uid)) {
            return false;
        }
        $condition = [];
        $condition['status'] = 3;
        $condition['user_id'] = $uid;
        $cols = 'money,create_time';
        $list = $this->oilPayOrderModel->field($cols)->where($condition)->order('create_time DESC')->select();
        if (!empty($list)) {
            $i = count($list);
            foreach ($list as $k => $v) {
                $list[$k]['sort'] = $i;
                $list[$k]['create_time'] = date("Y-m-d H:i:s", $v['create_time']);
                $i--;
            }
        }
        return $list;
    }
}